package com.cropdoctor.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cropdoctor.model.dto.SysUserDto;
import com.cropdoctor.model.po.SysUserPo;
import com.cropdoctor.model.vo.UserVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * Description: 【 用户表 数据库操作Mapper 】
 */
@Mapper
public interface SysUserMapper extends BaseMapper<SysUserPo> {

    // ==================== QUERY ====================


    /**
     * 描述: 【 通过ID查询单条数据 】
     */
    @Select("select t.* from sys_user t where t.id = #{id}")
    SysUserDto queryOneById(@Param("id") Long id);

    @Select("select * from sys_user where account = #{userName}")
    SysUserPo selectByName(String userName);

    @Select("select su.avatar_url,su.user_name,su.id,su.account,su.enabled,sr.role_name,sr.role_description\n" +
            "from sys_user su left join sys_user_role_relation sa\n" +
            "on su.id = sa.user_id\n" +
            "left join sys_role sr\n" +
            "on sa.role_id = sr.id where account = #{userName}")
    UserVO selectWithRoleByName(String userName);

    /**
     * 多表查询分页
     * @param page
     * @return
     */
    Page<UserVO> selectWithRole(IPage<SysUserPo> page, @Param(Constants.WRAPPER) Wrapper<SysUserPo> queryWrapper);
}
